﻿using Newtonsoft.Json;
using System; 
using System.Collections.Generic;
using System.Text; 
using UnityEngine;
using UnityEngine.Networking;
using XFABManager;

namespace XFGameFramework
{
    public class HttpManager
    {

        /// <summary>
        /// 接口请求前触发
        /// </summary>
        public static Action<UnityWebRequest> PreWebRequest;

        /// <summary>
        /// 接口请求后触发(参数一:请求对象，参数二:耗时)
        /// </summary>
        public static Action<UnityWebRequest,float> PostWebRequest;

        /// <summary>
        /// 发起Get请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="param">参数</param>
        /// <param name="headers">请求头</param>
        /// <returns></returns>
        public static SendWebRequest Get(string url, Dictionary<string, object> param, Dictionary<string, string> headers)
        {

            StringBuilder builderParam = new StringBuilder();
            if (param != null)
            {
                foreach (string key in param.Keys)
                {
                    if (builderParam.Length != 0)
                    {
                        builderParam.Append("&");
                    }
                    builderParam.Append(key).Append("=").Append(param[key]);
                }
            }

            if (builderParam.Length != 0)
            {
                url = string.Format("{0}?{1}", url, builderParam.ToString());
            }

            UnityWebRequest request = UnityWebRequest.Get(url);

            if (headers != null)
            {
                foreach (var key in headers.Keys)
                {
                    request.SetRequestHeader(key, headers[key]);
                }
            }
            return SendWebRequest(request);
        }

        /// <summary>
        /// 发起Post请求，参数以Form形式提交
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="param">参数</param>
        /// <param name="headers">请求头</param>
        /// <returns></returns>
        public static SendWebRequest PostForm(string url, Dictionary<string, string> param, Dictionary<string, string> headers)
        {
            WWWForm form = new WWWForm();
            if (param != null)
            {
                foreach (var key in param.Keys)
                {
                    form.AddField(key, param[key]);
                }
            }

            UnityWebRequest request = UnityWebRequest.Post(url, form);

            if (headers != null)
            {
                foreach (var item in headers.Keys)
                {
                    request.SetRequestHeader(item, headers[item]);
                }
            }


            return SendWebRequest(request);
        }


        /// <summary>
        /// 发起Post请求，参数以Body形式提交
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="param">参数</param>
        /// <param name="headers">请求头</param>
        /// <returns></returns>
        public static SendWebRequest PostBody(string url, Dictionary<string, object> param, Dictionary<string, string> headers)
        {

            UnityWebRequest request = UnityWebRequest.Post(url, string.Empty);

            byte[] bodyRaw = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(param));
            request.uploadHandler = new UploadHandlerRaw(bodyRaw);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.useHttpContinue = false;
            request.disposeUploadHandlerOnDispose = true;
            request.disposeDownloadHandlerOnDispose = true;
            if (headers != null && headers.Count != 0)
            {
                foreach (string key in headers.Keys)
                {
                    request.SetRequestHeader(key, headers[key]);
                }
            }
            return SendWebRequest(request);
        }


        /// <summary>
        /// 发起Put请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="param">参数</param>
        /// <param name="headers">请求头</param>
        /// <returns></returns>
        public static SendWebRequest Put(string url, Dictionary<string, object> param, Dictionary<string, string> headers)
        {
            UnityWebRequest request = UnityWebRequest.Put(url, string.Empty);

            byte[] bodyRaw = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(param));
            request.uploadHandler = new UploadHandlerRaw(bodyRaw);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.useHttpContinue = false;
            request.disposeUploadHandlerOnDispose = true;
            request.disposeDownloadHandlerOnDispose = true;
            //webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
            if (headers != null && headers.Count != 0)
            {
                foreach (string key in headers.Keys)
                {
                    request.SetRequestHeader(key, headers[key]);
                }
            }

            return SendWebRequest(request);
        }

        /// <summary>
        /// 发起Delete请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="param">参数</param>
        /// <param name="headers">请求头</param>
        /// <returns></returns>
        public static SendWebRequest Delete(string url, Dictionary<string, object> param, Dictionary<string, string> headers)
        {
            UnityWebRequest webRequest = UnityWebRequest.Delete(url);

            byte[] bodyRaw = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(param));
            webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
            webRequest.downloadHandler = new DownloadHandlerBuffer();
            webRequest.useHttpContinue = false;
            webRequest.disposeUploadHandlerOnDispose = true;
            webRequest.disposeDownloadHandlerOnDispose = true;
            if (headers != null && headers.Count != 0)
            {
                foreach (string key in headers.Keys)
                {
                    webRequest.SetRequestHeader(key, headers[key]);
                }
            }
            return SendWebRequest(webRequest);
        }

        internal static SendWebRequest SendWebRequest(UnityWebRequest request)
        {
            SendWebRequest send_web_request = new SendWebRequest();
            CoroutineStarter.Start(send_web_request.SendRequest(request));
            return send_web_request;
        }
    }

}
